package com.example.pingmiantutui.ui.mistake_collection

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.pingmiantutui.R
import com.example.pingmiantutui.data.MistakeQuestion
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup

abstract class BaseMistakeQuestionAdapter : ListAdapter<MistakeQuestion, RecyclerView.ViewHolder>(MistakeQuestionDiffCallback()) {

    companion object {
        const val VIEW_TYPE_SINGLE = 1
        const val VIEW_TYPE_CONTRAST = 2
        const val VIEW_TYPE_ANALYSE = 3
    }

    protected fun setTextViewVisibility(label: TextView, content: TextView, text: String?) {
        if (!text.isNullOrEmpty()) {
            label.visibility = View.VISIBLE
            content.visibility = View.VISIBLE
            content.text = text
        } else {
            label.visibility = View.GONE
            content.visibility = View.GONE
        }
    }

    protected fun setupTypeTags(group: ChipGroup, tags: List<String>) {
        group.removeAllViews()
        tags.forEach { tag ->
            val chip = Chip(group.context).apply {
                text = tag
                isCheckable = false
            }
            group.addView(chip)
        }
    }

    open inner class SingleQuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        protected val imageView: ImageView = itemView.findViewById(R.id.questionImage)
        protected val descriptionLabel: TextView = itemView.findViewById(R.id.descriptionLabel)
        protected val descriptionView: TextView = itemView.findViewById(R.id.description)
        protected val errorproneLabel: TextView = itemView.findViewById(R.id.errorproneLabel)
        protected val errorproneView: TextView = itemView.findViewById(R.id.errorprone)
        protected val typeTagsGroup: ChipGroup = itemView.findViewById(R.id.typeTagsGroup)

        open fun bind(question: MistakeQuestion) {
            val imageResourceId = itemView.context.resources.getIdentifier(
                question.imagePath,
                "drawable",
                itemView.context.packageName
            )
            imageView.setImageResource(if (imageResourceId != 0) imageResourceId else R.drawable.ic_menu_camera)

            setTextViewVisibility(descriptionLabel, descriptionView, question.description)
            setTextViewVisibility(errorproneLabel, errorproneView, question.errorprone)
            setupTypeTags(typeTagsGroup, question.typeTags)
        }
    }

    open inner class ContrastQuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        protected val imageView: ImageView = itemView.findViewById(R.id.questionImage)
        protected val question1Label: TextView = itemView.findViewById(R.id.question1Label)
        protected val description1View: TextView = itemView.findViewById(R.id.questionDescription1)
        protected val errorprone1Label: TextView = itemView.findViewById(R.id.errorprone1Label)
        protected val errorprone1View: TextView = itemView.findViewById(R.id.errorprone1)
        protected val question2Label: TextView = itemView.findViewById(R.id.question2Label)
        protected val description2View: TextView = itemView.findViewById(R.id.questionDescription2)
        protected val errorprone2Label: TextView = itemView.findViewById(R.id.errorprone2Label)
        protected val errorprone2View: TextView = itemView.findViewById(R.id.errorprone2)
        protected val similarityLabel: TextView = itemView.findViewById(R.id.similarityLabel)
        protected val similarityView: TextView = itemView.findViewById(R.id.similarity)
        protected val differenceLabel: TextView = itemView.findViewById(R.id.differenceLabel)
        protected val differenceView: TextView = itemView.findViewById(R.id.difference)
        protected val typeTagsGroup: ChipGroup = itemView.findViewById(R.id.typeTagsGroup)

        open fun bind(question: MistakeQuestion) {
            val imageResourceId = itemView.context.resources.getIdentifier(
                question.imagePath,
                "drawable",
                itemView.context.packageName
            )
            imageView.setImageResource(if (imageResourceId != 0) imageResourceId else R.drawable.ic_menu_camera)

            setTextViewVisibility(question1Label, description1View, question.description1)
            setTextViewVisibility(errorprone1Label, errorprone1View, question.errorprone1)
            setTextViewVisibility(question2Label, description2View, question.description2)
            setTextViewVisibility(errorprone2Label, errorprone2View, question.errorprone2)
            setTextViewVisibility(similarityLabel, similarityView, question.similarity)
            setTextViewVisibility(differenceLabel, differenceView, question.difference)
            setupTypeTags(typeTagsGroup, question.typeTags)
        }
    }

    open inner class AnalyseQuestionViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        protected val imageView: ImageView = itemView.findViewById(R.id.questionImage)
        protected val descriptionLabel: TextView = itemView.findViewById(R.id.descriptionLabel)
        protected val descriptionView: TextView = itemView.findViewById(R.id.description)
        protected val errorproneLabel: TextView = itemView.findViewById(R.id.errorproneLabel)
        protected val errorproneView: TextView = itemView.findViewById(R.id.errorprone)
        protected val analyseLabel: TextView = itemView.findViewById(R.id.analyseLabel)
        protected val analyseImage: ImageView = itemView.findViewById(R.id.analyseImage)
        protected val typeTagsGroup: ChipGroup = itemView.findViewById(R.id.typeTagsGroup)

        open fun bind(question: MistakeQuestion) {
            val imageResourceId = itemView.context.resources.getIdentifier(
                question.imagePath,
                "drawable",
                itemView.context.packageName
            )
            imageView.setImageResource(if (imageResourceId != 0) imageResourceId else R.drawable.ic_menu_camera)

            setTextViewVisibility(descriptionLabel, descriptionView, question.description)
            setTextViewVisibility(errorproneLabel, errorproneView, question.errorprone)

            if (!question.analyseImagePath.isNullOrEmpty()) {
                try {
                    val analyseImageResourceId = itemView.context.resources.getIdentifier(
                        question.analyseImagePath,
                        "drawable",
                        itemView.context.packageName
                    )
                    if (analyseImageResourceId != 0) {
                        analyseLabel.visibility = View.VISIBLE
                        analyseImage.apply {
                            setImageResource(analyseImageResourceId)
                            visibility = View.VISIBLE
                        }
                    } else {
                        analyseLabel.visibility = View.GONE
                        analyseImage.visibility = View.GONE
                    }
                } catch (e: Exception) {
                    analyseLabel.visibility = View.GONE
                    analyseImage.visibility = View.GONE
                }
            } else {
                analyseLabel.visibility = View.GONE
                analyseImage.visibility = View.GONE
            }

            setupTypeTags(typeTagsGroup, question.typeTags)
        }
    }
}

class MistakeQuestionDiffCallback : DiffUtil.ItemCallback<MistakeQuestion>() {
    override fun areItemsTheSame(oldItem: MistakeQuestion, newItem: MistakeQuestion): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: MistakeQuestion, newItem: MistakeQuestion): Boolean {
        return oldItem == newItem
    }
}